<?php
/**
* for insert image into DB with specified $_FILES as array
*@param table_name
*@param referer_id
*@param file the $_FILES array
*@param createthumb   [optional]  true | false , default false
*@param auto_thumb_size   [optional]
*@param auto_large_size  [optional]
*/
function InnoForum_adminblobapi_insertImageArray($args)
{
  Loader::loadClass('Thumbnail', "modules/InnoForum/pnincludes");
  
  $table_name   = $args['table_name'];
  $ref_id       = $args['referer_id'];
  $file         = $args['files'];
  $path         = $args['path'];

  $createthumb  = isset($args['createthumb']) ? $args['createthumb'] : false ;
  $auto_medium_size  = isset($args['auto_medium_size']) ? $args['auto_medium_size'] : UPLOAD_IMAGE_MEDIUM_SIZE;
  $auto_large_size = isset($args['auto_large_size']) ? $args['auto_large_size'] : UPLOAD_IMAGE_LARGE_SIZE  ;

  $limit_size = UPLOAD_IMAGE_LIMIT_SIZE ; //file size limit at 800kb
  $limit_file_type = UPLOAD_IMAGE_LIMIT_FILE_TYPE; //any image file type
  $image_size = UPLOAD_IMAGE_LARGE_SIZE;

  foreach ($file["error"] as $key => $error){
    if ($error == 0){

      $tmp_name = $file["tmp_name"][$key];
      $name = $file["name"][$key];
      $type = $file["type"][$key];
      $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));

      $size = @getimagesize($tmp_name);
      $width = $size[0];
      $height = $size[1];
      //resize if image size larger than $auto_large_size
      if ($width > $auto_large_size && $height > $auto_large_size){
        $photo = new Thumbnail($tmp_name,$ext);
        $photo->size_auto($auto_large_size);
        $width = $photo->getWidth();
        $height = $photo->getHeight();

        $photodata = $photo->exportRawData();
        unset($photo);
      }else{
        $photodata = file_get_contents($tmp_name);
      }
      if ($createthumb == true){
        $thumb = new Thumbnail($tmp_name,$ext);
        $thumb->size_auto($auto_medium_size);
        $thumbdata = $thumb->exportRawData();
        unset($thumb);
      }
      $object = array(
        'referer_id' => $ref_id,
        'filename'    => $name,
        'data_medium' => $thumbdata,
        'data_large'  => $photodata,
        'type'        => $type,
        'width'       => $width,
        'height'      => $height,
        'path'        => $path);
      if ($table_name && $ref_id){
        DBUtil::insertObject($object,$table_name);
      }
      unset($object);
      unset($thumbdata);
      unset($photodata);
    }
  }
}
/**
* for insert image into DB with specified $_FILES
*@param table_name
*@param referer_id
*@param file the $_FILES  or file path
*@param createthumb       [optional]  true | false , default false
*@param auto_medium_size  [optional]
*@param auto_large_size   [optional]
*/
function InnoForum_adminblobapi_insertImage($args)
{
  Loader::loadClass('Thumbnail', "modules/InnoForum/pnincludes");
  
  $table_name   = $args['table_name'];
  $referer_id  = $args['referer_id'];
  $file         = $args['files'];
  $path         = $args['path'];

  if (gettype($file) == 'string' ){
    $type = 'server';
  }else{
    $type = 'client';
  }
  $createthumb  = isset($args['createthumb']) ? $args['createthumb'] : false ;
  $auto_medium_size  = isset($args['auto_medium_size']) ? $args['auto_medium_size'] : UPLOAD_IMAGE_MEDIUM_SIZE;
  $auto_large_size = isset($args['auto_large_size']) ? $args['auto_large_size'] : UPLOAD_IMAGE_LARGE_SIZE  ;

  $limit_size = UPLOAD_IMAGE_LIMIT_SIZE ; //file size limit at 800kb
  $limit_file_type = UPLOAD_IMAGE_LIMIT_FILE_TYPE; //any image file type
  //set error flag to 0 if not client2/11/2551
  if ($type == 'client'){
    $error = $file["error"];
  }else{
    $error = 0 ;
  }
  if ($error == 0 ){
    if ($type == 'client'){
      $tmp_name = $file["tmp_name"];
      $name = $file["name"];
      $type = $file["type"];
      $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));
    }else{
      $tmp_name = $file;
      $name = $file;
      $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));
      if ($ext == 'jpg'){
        $ext = 'jpeg';
      }
      $type = "image/" . $ext;
    }
    

    $size = @getimagesize($tmp_name);
    $width = $size[0];
    $height = $size[1];
    //resize if image size larger than $auto_large_size
    if ($width  > $auto_large_size && $height > $auto_large_size){
      $photo = new Thumbnail($tmp_name,$ext);
      $photo->size_auto($auto_large_size);
      $width = $photo->getWidth();
      $height = $photo->getHeight();
      $photodata = $photo->exportRawData();
      unset($photo);
    }else{
      $photodata = file_get_contents($tmp_name);
    }
    if ($createthumb == true){
      $thumb = new Thumbnail($tmp_name,$ext);
      $thumb->size_auto($auto_medium_size);
      $thumbdata = $thumb->exportRawData();
      unset($thumb);
    }
    $object = array(
      'referer_id' => $referer_id,
      'filename'    => $name,
      'data_medium' => $thumbdata,
      'data_large'  => $photodata,
      'type'        => $type,
      'width'       => $width,
      'height'      => $height,
      'path'        => $path);
    if ($table_name && $referer_id){
      DBUtil::insertObject($object,$table_name);
    }
    unset($object);
    unset($thumbdata);
    unset($photodata);
  }
}
/**
* for insert image into DB with specified $_FILES
*@param table_name
*@param referer_id
*@param file the $_FILES
*@param auto_mini_size   [optional]
*@param auto_medium_size   [optional]
*@param auto_large_size  [optional]
*/
function InnoForum_adminblobapi_insertIcon($args)
{
  Loader::loadClass('Thumbnail', "modules/InnoForum/pnincludes");
  
  $table_name   = $args['table_name'];
  $referer_id  = $args['referer_id'];
  $file         = $args['files'];
  $path         = $args['path'];

  $createthumb  = isset($args['createthumb']) ? $args['createthumb'] : false ;
  $auto_mini_size  = isset($args['auto_mini_size']) ? $args['auto_mini_size'] : UPLOAD_ICON_MINI_SIZE;
  $auto_medium_size  = isset($args['auto_medium_size']) ? $args['auto_medium_size'] : UPLOAD_ICON_MEDIUM_SIZE;
  $auto_large_size = isset($args['auto_large_size']) ? $args['auto_large_size'] : UPLOAD_ICON_LARGE_SIZE  ;

  $limit_size = UPLOAD_IMAGE_LIMIT_SIZE ; //file size limit at 800kb
  $limit_file_type = UPLOAD_IMAGE_LIMIT_FILE_TYPE; //any image file type

  $error = $file["error"];
  if ($error == 0){

    $tmp_name = $file["tmp_name"];
    $name = $file["name"];
    $size = $file["size"];
    $type = $file["type"];
    $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));


    //large
    $large = new Thumbnail($tmp_name,$ext);
    $large->size_auto($auto_large_size);
    $data_large = $large->exportRawData();
    unset($large);
    //medium
    $medium = new Thumbnail($tmp_name,$ext);
    $medium->size_auto($auto_medium_size);
    $data_medium = $medium->exportRawData();
    unset($medium);
    //mini
    $mini = new Thumbnail($tmp_name,$ext);
    $mini->size_auto($auto_mini_size);
    $data_mini = $mini->exportRawData();
    unset($mini);


    $object = array(
      'referer_id' => $referer_id,
      'filename'    => $name,
      'data_mini'   => $data_mini,
      'data_medium' => $data_medium,
      'data_large'  => $data_large,
      'type'        => $type,
      'status'      => 1,
      'path'        => $path);
    if ($table_name && $referer_id){
      DBUtil::insertObject($object,$table_name);
    }
    unset($object);
    unset($data_mini);
    unset($data_medium);
    unset($data_large);
  }
}
/**
* for insert model into DB with specified $_FILES
*@param table_name
*@param referer_id
*@param file the $_FILES
*/
function InnoForum_adminblobapi_insertModel($args)
{
  Loader::loadClass('SimpleUnzip', "modules/InnoForum/pnincludes");
  Loader::loadClass('Thumbnail', "modules/InnoForum/pnincludes");
  //Loader::loadClass('XMLParser', "modules/InnoForum/pnincludes");
  Loader::loadClass('SAXReader', "modules/InnoForum/pnincludes");
  
  $table_name   = $args['table_name'];
  $referer_id  = $args['referer_id'];
  $file         = $args['files'];
  $path         = $args['path'];

  $error = $file["error"];
  if ($error == 0){

    $tmp_name = $file["tmp_name"];
    $name = $file["name"];
    $size = $file["size"];
    $type = $file["type"];
    $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));
    $data = file_get_contents($tmp_name);
    //extract doc.xml to get model information
    $unzip = new SimpleUnzip($tmp_name);
    $doc = $unzip->GetDataByFileName('doc.kml');
    /*
    $parser = new XMLParser($doc, 'raw', 1);
    $info = serialize($parser->getTree());
    */
    $sax = new SAXReader();
    $sax->setXml($doc);
    $info = serialize($sax->readNextNode("LookAt"));
    $sax->close();

    unset($unzip);
    unset($doc);
    unset($parser);

    $object = array(
      'referer_id' => $referer_id,
      'filename'    => $name,
      'data'        => $data,
      'info'        => $info,
      'type'        => $type,
      'status'      => 1,
      'path'        => $path);
    if ($table_name && $referer_id){
      DBUtil::insertObject($object,$table_name);
    }
    unset($object);
    unset($data);
    unset($doc_info);
  }
}
/**
* for insert flash video into DB with specified $_FILES
*@param table_name
*@param referer_id
*@param file the $_FILES
*/
function InnoForum_adminblobapi_insertVideo($args)
{
  Loader::loadClass('Thumbnail', "modules/InnoForum/pnincludes");
  Loader::loadClass('FLVInfo2', "modules/InnoForum/pnincludes/flashmeta");

  $table_name   = $args['table_name'];
  $referer_id  = $args['referer_id'];
  $file         = $args['files'];
  $path         = $args['path'];

  $error = $file["error"];
  if ($error == 0){

    $tmp_name = $file["tmp_name"];
    $name = $file["name"];
    $size = $file["size"];
    $type = $file["type"];
    $ext = strtolower(ereg_replace(".*\.(.*)$","\\1",$name));
    $data = file_get_contents($tmp_name);

    // Get FLV information
    $flvinfo = new FLVInfo2();
    $info = $flvinfo->getInfo($tmp_name,true,true);

    $object = array(
      'referer_id' => $referer_id,
      'filename'    => $name,
      'data'        => $data,
      'type'        => $type,
      'duration'    => $info->duration,
      'width'       => $info->video->width,
      'height'      => $info->video->height,
      'bitrate'     => $info->video->bitrate,
      'fps'         => $info->video->fps,
      'status'      => 1,
      'path'        => $path);

    if ($table_name && $referer_id){
      DBUtil::insertObject($object,$table_name);
    }
    unset($object);
    unset($data);
    unset($info);
    unset($flvinfo);
  }
}